package com.peanutplan.blog.constant;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.peanutplan.blog.interceptor.BaseInterceptor;
import com.peanutplan.blog.utils.CommonUtil;

@EnableWebMvc
@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter{
 
	@Resource
    private BaseInterceptor baseInterceptor;
	
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(baseInterceptor);
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    	//将所有/** 访问都映射到classpath:/ 目录下
        //也可配置/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

        /** 文件上传路径 */
        registry.addResourceHandler("/profile/**").addResourceLocations("file:" + WebConst.defaultProfile);
        
        /** 文件上传路径 */
        registry.addResourceHandler("/cover/**").addResourceLocations("file:" + WebConst.defaultArticleCover);
    
        /** 添加静态资源文件，外部可以直接访问地址*/
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ CommonUtil.getUplodFilePath()+"upload/");
    }
}